<!-- 用户准备下单时点击选择优惠券 -->
<template>
	<view class="page-total">
		<view class="cu-modal bottom-modal" :class="{ show: isShow }" @click="hide">
			<view class="cu-dialog">
				<view class="coupon-title">
					<view class="title">优惠券</view>
					<view class="explain">使用说明</view>
				</view>
				<view class="coupon-tab">
					<view class="tab" :class="{'action':TabShow===0}" @click.stop="onTab(0)">
						<text>可用优惠券</text>
						<text class="line"></text>
					</view>
					<view class="tab" :class="{'action':TabShow===1}" @click.stop="onTab(1)">
						<text>不可用优惠券</text>
						<text class="line"></text>
					</view>
				</view>
				<!-- 优惠券数据 -->
				<view class="coupon-data">
					<view class="coupon-list">
						<view class="list" :class="{'forbidden':TabShow === 1}" v-for="(item,index) in Coupon"
							:key="index">
							<view class="coupon-price">
								<view class="discounts">
									<text class="min">￥</text>
									<text class="max">{{item.price}}</text>
								</view>
								<view class="full-reduction">
									<text>满{{item.full}}元减{{item.price}}元</text>
								</view>
								<view class="jag"></view>
							</view>
							<view class="coupon-info">
								<view class="info-title">
									<view class="tag">
										<text>全品类券</text>
									</view>
									<view class="title">
										<text>可购买所有商品</text>
									</view>
								</view>
								<view class="date-get">
									<view class="date">
										<text>{{Coupon[index].getTime}}</text>
									</view>
									<view class="check" v-show="TabShow === 0">
										<checkbox class="real_name_authentication_36" :checked="item.checked"
											:value="item.id" @click="checkboxChange(index,item.price)" />
										<evaluate ref="test" />
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
				<!--确认 -->
				<view class="cpupon-confirm">
					<view class="confirm" @click="onConfirm">确定</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		baseUrl
	} from '@/config/baseUrl.js';

	export default {
		props:["objdata"],
		data() {
			return {
				// someShow: true,
				isShow: false,
				TabShow: 0,
				Coupon: [],
				CouponId: 0,
				checkState: false,
				couponPrice: 0,
				updateFunc:'',
				couponInfo:{
					couponPrice:'',
					CouponId:'',
					CouponNumber:'',
				},
				types:'',
			};
		},
		onLoad() {
			// this.refresh();
		},
		created:function(){
			this.types=this.objdata.type;
			console.log(this.objdata)//打印父组件传过来的参数
			console.log(this.types)
		},
		methods: {
			checkboxChange(index,price) {
				for (var i = 0; i < this.Coupon.length; i++) {
					if(i!=index){
						this.Coupon[i].checked = false;
					}
				}		
				this.Coupon[index].checked=!this.Coupon[index].checked	
				console.log("当前"+index+"优惠券状态："+this.Coupon[index].checked)
				console.log("当前"+index+"优惠券面值："+price)
				this.couponPrice=price;
				this.couponInfo.CouponNumber=index;
				// //加入数组
				this.CouponId=this.Coupon[index].couponId;
				console.log("选择的优惠券id："+this.CouponId)
				if(!this.Coupon[index].checked){
					this.couponPrice=0;
					this.CouponId=0;
					console.log("X当前"+index+"优惠券状态："+this.Coupon[index].checked)
					console.log("X当前"+index+"优惠券面值："+this.couponPrice)
					console.log("X选择的优惠券id："+this.CouponId)
				}
			},
			show(id) {				
				this.Coupon = [];
				/* 获取用户可用的优惠券 */
				uni.request({
					url: baseUrl + "/user/canUseCoupon",
					method: 'POST',
					header: {
						"Content-Type": "application/x-www-form-urlencoded",
					},
					data: {
						userId: uni.getStorageSync("userId"),
						price: uni.getStorageSync("price"),
					},
					success: (res) => {
						console.log("可使用的优惠券:", res.data.canUseCoupon)
						this.Coupon = res.data.canUseCoupon
						for (var i = 0; i < this.Coupon.length; i++) {
							this.$set(this.Coupon[i], 'checked', false);
						}
						if(id!=-1){
							this.Coupon[id].checked = true;
						}
					}
				})
				console.log("id"+id)
				this.isShow = true;
			},
			hide() {
				// this.isShow = false;
			},
			/**
			 * tab 点击
			 */
			onTab(index) {
				if (index == 0) {
					this.Coupon.length = 0;
					/* 获取用户可用的优惠券 */
					uni.request({
						url: baseUrl + "/user/canUseCoupon",
						method: 'POST',
						header: {
							"Content-Type": "application/x-www-form-urlencoded",
						},
						data: {
							userId: uni.getStorageSync("userId"),
							price: uni.getStorageSync("price"),
						},
						success: (res) => {
							console.log("可使用的优惠券:", res.data.canUseCoupon)
							this.Coupon = res.data.canUseCoupon
						}
					})
				} else if (index == 1) {
					/* 获取用户不可用的优惠券 */
					uni.request({
						url: baseUrl + "/user/notUseCoupon",
						method: 'POST',
						header: {
							"Content-Type": "application/x-www-form-urlencoded",
						},
						data: {
							userId: uni.getStorageSync("userId"),
							price: uni.getStorageSync("price"),
						},
						success: (res) => {
							console.log("不可使用的优惠券:", res.data.notUseCoupon)
							this.Coupon = res.data.notUseCoupon
						}
					})
				}
				this.TabShow = index;
			},
			/**
			 * 确认点击
			 */
			onConfirm() {
				this.isShow = false;
				let a =false;
				for(let i=0;i < this.Coupon.length; i++){
					if(this.Coupon[i].checked){
						a = true
					}
				}
				if(a){
					this.couponInfo.couponPrice = this.couponPrice
					this.couponInfo.CouponId = this.CouponId
					uni.$emit('onConfirm',JSON.stringify(this.couponInfo))
				}else{
					this.couponInfo.couponPrice =''
					this.couponInfo.CouponId = ''
					uni.$emit('onConfirm',JSON.stringify(this.couponInfo))
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	@import 'UseCoupon.scss';
</style>
